package com.example.komunikator;



import android.app.ListActivity;
import android.view.Menu;

import java.io.UnsupportedEncodingException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.sql.DriverManager;
import android.database.SQLException;
import java.sql.Connection; 
import android.widget.Toast;
import java.util.ArrayList;
import android.widget.AdapterView;
import android.view.MenuInflater;
import android.view.ContextMenu.ContextMenuInfo;






public class UserList extends ListActivity  {
	private Handler mHandler = new Handler();
	private static final String url = "jdbc:mysql://mysql.agh.edu.pl:3306/rybczyns";
	private static final String user = "rybczyns";
	private static final String pass = "6gtZxjms";
	int ilosc_wierszy;
	ArrayAdapter<String> adapter;


	public void onCreate(Bundle icicle) throws SQLException {
		super.onCreate(icicle);



		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);


		String temp = null;
		try {  
			Class.forName("com.mysql.jdbc.Driver");  
			Connection con = DriverManager.getConnection(url, user, pass);
			//			Connection con = SQLLogic.getInstance().getConnection();
			SQLLogic.getInstance().setConnection(con);
			Statement st1 = con.createStatement();  
			ResultSet rs = st1.executeQuery("select count(mail) from Users");


			while(rs.next()){
				 ilosc_wierszy = rs.getInt(1);
			}


			Statement st = con.createStatement();			
			rs = st.executeQuery("select mail from Users");  


			String values[]= new String[ilosc_wierszy];

			//			Log.i("DEBUG1",rs.getMetaData().getColumnCount()-1.toString());


			Log.i("asd","Przed rs.wasNull");
			if(rs.wasNull()){
				Log.i("DEBUG","Statement is nulL!");
			}else{
				Log.i("asd","Przed While'em");
							int i=0;
							while(rs.next()) {
//				for (int i = 0;i<ilosc_wierszy;i++){

					temp=rs.getString("mail");
					values[i] = new String(temp);
					i++;
				}  
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, values);
			setListAdapter(adapter);	  

		} catch (NullPointerException e) { 
			e.printStackTrace();
		} catch (java.sql.SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException ex){
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		}



		registerForContextMenu(getListView());

	}





	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		Bundle b = getIntent().getExtras();
		String xmppConn="";

		if (b!= null){

			xmppConn = b.getString("xmppConn");
		}


		String item = (String) getListAdapter().getItem(position);

		Intent intent = new Intent(UserList.this, SingleChatWindow.class);
		intent.putExtra("mRecipient", item);
		intent.putExtra("xmppConn", xmppConn);
		startActivity(intent);     



		// Toast.makeText(this, item + " selected wartosc B: "+ someString, Toast.LENGTH_LONG).show();




	}
} 